package com.ymtms.settlement.controller;

import com.ymtms.settlement.pojo.CollectionBusinessParameters;
import com.ymtms.settlement.service.CollectionBusinessParametersService;
import com.ymtms.settlement.vo.GridResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/business/collection")
@Controller
public class CollectionBusinessParametersController {
    @Autowired
    CollectionBusinessParametersService collectionBusinessParametersService;

    @RequestMapping("/cbplist")
    @ResponseBody
    public GridResult<CollectionBusinessParameters> listCbp(){
        GridResult result = collectionBusinessParametersService.queryAllCbp();
        result.setCode(0);
        result.setMsg("");
        return result;
    }

   /* @RequestMapping("/edit")
    @ResponseBody
    public Map edit(CollectionBusinessParameters collectionBusinessParameters){
        Map result = new HashMap();
        // 1. 对前台传来的数据进行基本验证和修改
        if(collectionBusinessParameters == null){
            result.put("code",500);
            result.put("msg","数据传输错误");
            return result;
        }
        *//*由于前台传来的手续费率是百分比形式，与数据库形式不相同，需要转化*//*
        if(collectionBusinessParameters.getCommisionRateRuling() != null){
            collectionBusinessParameters.setCommisionRateRuling(
                    collectionBusinessParameters.getCommisionRateRuling()/100
            );
        }
        // 2. 执行添加
        try {
            collectionBusinessParametersService.update(collectionBusinessParameters);
            result.put("code",0);
            result.put("msg","修改成功");
        }catch(Exception e){
            result.put("code",500);
            result.put("msg",e.getMessage());
        }
        return result;
    }

    @RequestMapping("/add")
    @ResponseBody
    public Map add(CollectionBusinessParameters collectionBusinessParameters){
        Map result = new HashMap();
        // 1. 对前台传来的数据进行基本验证和修改
        if(collectionBusinessParameters == null){
            result.put("code",500);
            result.put("msg","数据传输错误");
            return result;
        }

        *//*由于前台传来的手续费率是百分比形式，与数据库形式不相同，需要转化*//*
        if(collectionBusinessParameters.getCommisionRateRuling() != null){
            collectionBusinessParameters.setCommisionRateRuling(
                    collectionBusinessParameters.getCommisionRateRuling()/100
            );
        }
        // 2. 执行添加
        try {
            collectionBusinessParametersService.save(collectionBusinessParameters);
            result.put("code",0);
            result.put("msg","新增成功");
        }catch(Exception e){
            result.put("code",500);
            result.put("msg",e.getMessage());
        }
        return result;
    }
    @RequestMapping("/delete/{cbpid}")
    @ResponseBody
    public Map RemoveByCbpid(@PathVariable("cbpid")Integer cbpid){
        Map result = new HashMap();
        try {
            collectionBusinessParametersService.removeByCbpid(cbpid); // 删除
            // 清空指定的redis缓存
//            Set<String> key = redisTemplate.keys("goodsbillMaster::*");
//            redisTemplate.delete(key);
            result.put("code", "0");
            result.put("msg", "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", "500");
            result.put("msg", "删除失败");
        }
        return result;
    }

    @RequestMapping("/changeStatus")
    @ResponseBody
    public Map changeStatus(@RequestParam("cbpid")Integer cbpid, @RequestParam("status")Integer status){
        Map result = new HashMap();
        try {
            collectionBusinessParametersService.updateStatusByCbpid(cbpid, status); // 删除
            // 清空指定的redis缓存
//            Set<String> key = redisTemplate.keys("goodsbillMaster::*");
//            redisTemplate.delete(key);
            result.put("code", "0");
            result.put("msg", "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", "500");
            result.put("msg", "操作失败");
        }
        return result;
    }*/
}
